package com.ruoyi.web.sso.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.web.sso.common.util.BusinessException;
import com.ruoyi.web.sso.common.util.ErrorCode;

public class StringUtil {

    public static String getSubStr(String str, int num,String index1,String index2) {
        String result = "";
        int i = 0;
        int last;
        int first;
        while(i < num) {
//            if(i==num-1)//保留第num个'{'和倒数第num个'}'，如果不需要，则只执行else里的代码就行
//            {last = str.lastIndexOf(index1)+1;
//                first = str.indexOf(index2);
//            }
//            else {
                last = str.lastIndexOf(index1);
                first = str.indexOf(index2)+1;
//            }
            str = str.substring(first, last);
            i++;
        }
        return str;
    }

    public static ObjectMapper objectMapper = new ObjectMapper();
    public static String toJson(Object o){
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new BusinessException(ErrorCode.UNDEFINED, "json转换出错");
        }
    }
}
